package com.guodexian.xiaomi.controller;

import com.guodexian.xiaomi.entity.Category;
import com.guodexian.xiaomi.service.CategoryService;
import com.guodexian.xiaomi.service.impl.CategoryServiceImpl;
import com.guodexian.xiaomi.util.HttpResponseUtil;
import com.guodexian.xiaomi.vo.ResultVO;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;

@WebServlet("/category/add")
public class CategoryAddController extends HttpServlet {
    private CategoryService categoryService = new CategoryServiceImpl();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
        Category category = new Category();
        try{
            BeanUtils.populate(category, req.getParameterMap());
        }  catch (Exception e) {
            throw new RuntimeException(e);
        }

        category.setCreate_time(new Date());
        ResultVO resultVO = categoryService.saveCategory(category);
        HttpResponseUtil.response(resp, resultVO);
    }
}
